home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1211 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template Class Linkage?
  5. Date: 09 Jan 1996 21:47:28 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan9164728@g7240065.bridge.bst.bls.com>
  8. References: <4cmnjs$mk7@mimsy.cs.umd.edu> <4ct9i4$i74@isoit109.bbn.hp.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Manfred_Lange's message of 9 Jan 1996 08:38:28 GMT
  11.  
  12. In article <4ct9i4$i74@isoit109.bbn.hp.com> Manfred_Lange <Manfred_Lange@bbn.hp.com> writes:
  13.  
  14. : gilman@cfar.umd.edu (Gil Carmel) wrote:
  15.  
  16. :> I'm just learning templates, and I'm having problems with linkage.
  17. :> When the template class member function bodies are included in the ".h" file,
  18. :> everything is fine.  But when the function bodies are in a separate ".c" file,
  19. :> calls to these functions from other files give an "undefined reference to
  20. :> function" error from the linker.  Do template class member functions have 
  21. :> to be in the ".h" file, or is this a problem with my compiler?
  22.  
  23. :> [snipped]
  24.  
  25. : My understanding is that the implementation of template classe member functions 
  26. : need to be in a file which is NOT the cpp-file. Two approaches are taken 
  27. : normally:
  28.  
  29. : 1. Put the function bodies in the header file.
  30.  
  31. : 2. Create a third file and include that one into the source files, where the 
  32. : template is being used.
  33.  
  34. : I personally prefer the first approach, although the second has the advantage 
  35. : of separating the declaration from the implementation.
  36.  
  37. This is all dependant on you compiler. Some compilers do template
  38. instantiation(sp?) at link time namely ptlink with the 
  39. CenterLine compiler and HP compiler and others.
  40.  
  41. It requires the template source code (in .c files) to reside in the same
  42. directory as the header, but does not need the source code available at compile
  43. time but establishes missing symbols after the initial link and tries
  44. to resolve these missing symbols by instantiating the necessary template
  45. functions.
  46.  
  47. The problem with having the instantiation at compile time is you get
  48. code bloat where 2 different modules instantiate the same template,
  49. unless duplicate code removal is performed at the end.
  50.  
  51. There are other template instantiation procedures and they are
  52. changing/improving all the time.
  53.  
  54. Regards
  55.  
  56.   -A.
  57.  
  58.  
  59. -- 
  60. | A.Champion                |
  61.